#include "xparameters.h"
#include <stdio.h>
#include "xuartps_hw.h"
#include "netif/xadapter.h"
#include "xstatus.h"
#include "xiic.h"
#include "xil_exception.h"
#include "xscugic.h"

extern int test_Xadc();
extern int test_pex8624();
extern int test_bcm5396();
extern int test_cps1848();
extern int test_lwip();
extern int show_bar0();
extern int read_config();
extern int IicPsIpmbExample(u16 DeviceId,u8 *RecDataPtr, u8 *SendDataPtr);
extern struct netif *echo_netif;
extern int global_status;
extern int IicSendIpmi();
extern void delay_SL( u32 delayCount);
extern void iface_ipmb_process_i2c_ring();
extern void app_process_main_ring(void);

#define BUFFER_SIZE	32

extern u8 SendBuffer[BUFFER_SIZE];	/* Buffer for Transmitting Data */
extern u8 RecvBuffer[BUFFER_SIZE];	/* Buffer for Receiving Data */

#define IIC1_DEVICE_ID		XPAR_XIICPS_1_DEVICE_ID

int global_i2c_send_flag=1;

extern int global_bit_send;


void ShowTab()
{
	xil_printf ("\r\n---------------Entering HR2 Module Test Mode-----------------\r\n");
	xil_printf("Keyboard-(0)	select Show Cpu configuration.\r\n");
	xil_printf("Keyboard-(1)	select Test xadc.\r\n");
	xil_printf("Keyboard-(2)	select Show Ethernet switch Bcm5396 register.\r\n");
	xil_printf("Keyboard-(3)	select Show_PCIe bridge Pex8624 register.\r\n");
	xil_printf("Keyboard-(4)	select Show SRIO switch Cps1848 register.\r\n");
	xil_printf("Keyboard-(5)	select Ping zynq ethernet IP: 192.168.100.10.\r\n");
	xil_printf("Keyboard-(6)	select Show tx/rx_req.\r\n");
	xil_printf("Keyboard-(7)	choose pl i2c send.\r\n");
	xil_printf("Keyboard-(8)	choose ps i2c send.\r\n");
	xil_printf("Keyboard-(9)	choose ps and pl i2c send.\r\n");
	xil_printf("Keyboard-(a)	choose ps(not init) and pl i2c send.\r\n");
	xil_printf("Keyboard-(b)	init pl i2c.\r\n");
	xil_printf("Keyboard-(q)(Q) Quit.\r\n");
	xil_printf("-------------------------------------------------------------\r\n");
	xil_printf("->");
}



void SlectUartMenu(void)
{
	u32 _regData;
	char *str=0;
	char cmd[4];
	u8 _recvData;
	u32 _bRunning=0;
	int status;
	int i=0;

	_recvData = '0';
	_bRunning = FALSE;
	/*
	 * Run the polled example, specify the Device ID that is
	 * generated in xparameters.h.
	 */

	xil_printf("\r\n--- keyboard the 'e' entering Test Mode --- \r\n");

	_regData = XUartPs_ReadReg(XPAR_XUARTPS_0_BASEADDR, XUARTPS_CR_OFFSET);

	/*
	 * Enable TX and RX for the device
	 */
	XUartPs_WriteReg(XPAR_XUARTPS_0_BASEADDR, XUARTPS_CR_OFFSET,
			  ((_regData & ~XUARTPS_CR_EN_DIS_MASK) | XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN));

	str = &(cmd[0]);


	IicPsIpmbExample(IIC1_DEVICE_ID,RecvBuffer,SendBuffer);

	while(1)
	{

		if(!XUartPs_IsReceiveData(XPAR_XUARTPS_0_BASEADDR))
		{
			test_Xadc();

			status=IicSendIpmi();

#if 0
			iface_ipmb_process_i2c_ring();
			app_process_main_ring();


			if(global_bit_send==1)
			{
				status=IicSendIpmi();
				if(global_status==0)
				{
					xil_printf("reset i2c controller\r\n");
					IicPsIpmbExample(IIC1_DEVICE_ID,RecvBuffer,SendBuffer);
				}
				global_bit_send=0;
			}
#endif

#if 0
			if(global_i2c_send_flag==1)
			{
				status=PLIicSendIpmi();

				status=IicSendIpmi();
				if(global_status==0)
				{
					IicPsIpmbExample(IIC1_DEVICE_ID,RecvBuffer,SendBuffer);
				}

			}
			else if(global_i2c_send_flag==2)
			{
				status=PLIicSendIpmi();
			}
			else if(global_i2c_send_flag==3)
			{
				status=IicSendIpmi();
				if(global_status==0)
				{
					IicPsIpmbExample(IIC1_DEVICE_ID,RecvBuffer,SendBuffer);
				}
			}
			else if(global_i2c_send_flag==4)
			{
				status=IicSendIpmi();
				status=PLIicSendIpmi();

			}
#endif
		}
		else
		{
			_recvData = XUartPs_ReadReg(XPAR_XUARTPS_0_BASEADDR, XUARTPS_FIFO_OFFSET);
			if (_recvData == 'e'|| _recvData == 'E')
			{
				_bRunning = TRUE;
				ShowTab();
			}
		}
		while(_bRunning)
		{
			str = &(cmd[0]);
			//_recvData = XUartPs_ReadReg(XPAR_XUARTPS_0_BASEADDR, XUARTPS_FIFO_OFFSET);
			_recvData = XUartPs_RecvByte(XPAR_XUARTPS_0_BASEADDR);
			while (_recvData != '\r' && _recvData != '\n')
			{
			    if (_recvData == '\b')
			    {
					if (str != &(cmd[0]))
					{
						str--;
					}
				}
				else
				{
					*str = _recvData ;
					str++ ;
				}
			    XUartPs_SendByte(STDOUT_BASEADDRESS, _recvData);
			    _recvData = XUartPs_RecvByte(STDIN_BASEADDRESS);
			}
			*str = '\0' ;
			switch (cmd[0])
			{
			   case '\0':
			   {
					   ShowTab();
					   break;
			   }
			   case 'q':
			   case 'Q':
			   {
					xil_printf("\r\nExit debug menu,Now entering work mode!\r\n");
					xil_printf("--- keyboard the 'e' entering Test Mode --- \r\n");
					xil_printf("->");
					cmd[0] = '0';
					_bRunning = FALSE;
					break;
			   }
			   case '0':
			   {
					read_config();
					xil_printf("->");
					break;
			   }
			   case '1':
			   {
					test_Xadc();
					xil_printf("->");
					break;
			   }
			   case '2':
			   {
					test_bcm5396();
					xil_printf("->");
					break;
			   }
			   case '3':
			   {
				   test_pex8624();
				   xil_printf("->");
				   break;
			   }
			   case '4':
			   {
					test_cps1848();
					xil_printf("->");
					break;
			   }
			   case '5':
			   {
					xil_printf("\r\n Ping zynq ethernet IP: 192.168.100.10\r\n");
					while(!XUartPs_IsReceiveData(XPAR_XUARTPS_0_BASEADDR))
					{
					  xemacif_input(echo_netif);
					}
					xil_printf("->");
					break;
			   }
			   case '6':
			   {
				   show_bar0();
				   xil_printf("->");
				   break;
			   }
			   case '7':
			   {
				   global_i2c_send_flag=2;
				   xil_printf("->");
				   break;
			   }
			   case '8':
			   {
				   global_i2c_send_flag=3;
				   xil_printf("->");
				   break;
			   }
			   case '9':
			   {
				   global_i2c_send_flag=1;
				   xil_printf("->");
				   break;
			   }
			    case 'a':
			   {
				   global_i2c_send_flag=4;
				   xil_printf("->");
				   break;
			   }
				case 'b':
			   {
				   IicPlInit();
				   xil_printf("->");
				   break;
			   }
			}
		}
	}
}
